package com.zhang.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.server.common.Result;
import com.zhang.server.controller.request.BookPageRequest;
import com.zhang.server.entity.Book;
import com.zhang.server.mapper.BookMapper;
import com.zhang.server.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService implements IBookService {
    @Autowired
    BookMapper bookMapper;

    @Override
    public List<Book> list() {
        return bookMapper.list();
    }

    @Override
    public Object page(BookPageRequest bookPageRequest) {
        // 将pageNum、pageSize作为参数传进方法
        PageHelper.startPage(bookPageRequest.getPageNum(), bookPageRequest.getPageSize());
        List<Book> bookList = bookMapper.listByCondition(bookPageRequest);

        System.out.println(bookList.get(0));
        // 将查询结果放到这里,返回，就会自动分页
        return new PageInfo<>(bookList);
    }

    @Override
    public Integer save(Book book) {
        return bookMapper.save(book);
    }

    @Override
    public Result getById(Integer id) {
        return Result.success(bookMapper.getById(id));
    }

    @Override
    public void update(Book book) {
        bookMapper.update(book);
    }

    @Override
    public void delete(Integer id) {
        bookMapper.delete(id);
    }

    @Override
    public Book getByBookNo(String bookNo) {
        return bookMapper.getByBookNo(bookNo);
    }
}
